﻿Public Class Frm_at3ab
    Public dv As Dossier
    Public avo As Montant

    Public Sub total()
        Dim sum As Double = 0
        For Each khib As Montant In dv.List_montant
            If khib.Type = "المبلغ المطلوب" Then
                sum = sum + khib.Montant
            End If
        Next
        txt_total_requi.Text = sum
        '----------------------------------
        sum = 0
        For Each khib As Montant In dv.List_montant
            If khib.Type = "المبلغ المودع" Then
                sum = sum + khib.Montant
            End If
        Next
        txt_total_moda3.Text = sum
        '----------------------------------------
        If txt_total_moda3.Text <> "" And txt_total_requi.Text <> "" Then
            txt_rest.Text = txt_total_requi.Text - txt_total_moda3.Text
        End If
    End Sub


    Private Sub Frm_at3ab_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.close()
        Partager.nonInitial = New ArrayList
        Partager.nonInitial.Add("Comb_marji3ona")
        Partager.remplirDossier(Comb_marji3ona)
        
    End Sub

    Private Sub txt_mablagh_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_mablagh.KeyPress
        Dim allowedChars As String = "1234567890,"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub txt_mablagh_moda3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_mablagh_moda3.KeyPress
        Dim allowedChars As String = "1234567890,"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click

        If Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            Try
                If txt_mablagh.Text <> "" Then

                    If MsgBox("هل تريد فعلا إضافة هذا المبلغ ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة الأتعاب") = MsgBoxResult.Yes Then
                        Partager.close()

                        Dim cli As New Montant(1, txt_mablagh.Text, Date_daf3.Value, txt_molahat.Text, "المبلغ المطلوب")
                        dv.Ajouter_montant(cli)

                        List_tous_matloub.Items.Clear()
                        For Each khib As Montant In dv.List_montant
                            If khib.Type = "المبلغ المطلوب" Then
                                List_tous_matloub.Items.Add(New ListViewItem(New String() {khib.Id, khib.Montant, khib.Date_payement, khib.Remarque}))
                            End If
                        Next

                        MsgBox("تمت إضافة المبلغ بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me, Partager.nonInitial)
                        total()
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If


            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

   

    Private Sub btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_supp.Click
        If List_tous_matloub.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا المبلغ ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف الأتعاب") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim clt As Montant = dv.List_montant(List_tous_matloub.SelectedIndices.Item(0))
                    dv.suprimmer_montant(avo.Id)

                    List_tous_matloub.Items.Clear()
                    For Each khib As Montant In dv.List_montant
                        If khib.Type = "المبلغ المطلوب" Then
                            List_tous_matloub.Items.Add(New ListViewItem(New String() {khib.Id, khib.Montant, khib.Date_payement, khib.Remarque}))
                        End If
                    Next
                    Partager.vider(Me, Partager.nonInitial)
                    total()
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")

                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر المبلغ من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub List_tous_matloub_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous_matloub.SelectedIndexChanged
        If List_tous_matloub.SelectedItems.Count > 0 Then
            Try
                Partager.close()
                Dim i As Integer = 0
                Dim id As Integer = CType(List_tous_matloub.SelectedItems(0).Text, Integer)
                Dim j As Integer = 0
                While i < List_tous_matloub.SelectedIndices.Count
                    avo = dv.List_montant(j)
                    If avo.Id = id Then
                        i += 1
                    End If
                    j += 1
                End While
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub btn_ajout_moda3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajout_moda3.Click
        If Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            Try
                If txt_mablagh_moda3.Text <> "" Then

                    If MsgBox("هل تريد فعلا إضافة هذا المبلغ ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة الأتعاب") = MsgBoxResult.Yes Then
                        Partager.close()

                        Dim cli As New Montant(1, txt_mablagh_moda3.Text, Date_daf3_moda3.Value, txt_molahat_moda3.Text, "المبلغ المودع")
                        dv.Ajouter_montant(cli)

                        List_tous_mouda3.Items.Clear()
                        For Each khib As Montant In dv.List_montant
                            If khib.Type = "المبلغ المودع" Then
                                List_tous_mouda3.Items.Add(New ListViewItem(New String() {khib.Id, khib.Montant, khib.Date_payement, khib.Remarque}))
                            End If
                        Next

                        MsgBox("تمت إضافة المبلغ بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me, Partager.nonInitial)
                        total()
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If


            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub List_tous_mouda3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous_mouda3.SelectedIndexChanged
        If List_tous_mouda3.SelectedItems.Count > 0 Then
            Try
                Partager.close()
                Dim i As Integer = 0
                Dim id As Integer = CType(List_tous_mouda3.SelectedItems(0).Text, Integer)
                Dim j As Integer = 0
                While i < List_tous_mouda3.SelectedIndices.Count
                    avo = dv.List_montant(j)
                    If avo.Id = id Then
                        i += 1
                    End If
                    j += 1
                End While
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub btn_supp_moda3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_supp_moda3.Click
        If List_tous_mouda3.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا المبلغ ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف الأتعاب") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim clt As Montant = dv.List_montant(List_tous_mouda3.SelectedIndices.Item(0))
                    dv.suprimmer_montant(avo.Id)

                    List_tous_mouda3.Items.Clear()
                    For Each khib As Montant In dv.List_montant
                        If khib.Type = "المبلغ المودع" Then
                            List_tous_mouda3.Items.Add(New ListViewItem(New String() {khib.Id, khib.Montant, khib.Date_payement, khib.Remarque}))
                        End If
                    Next
                    Partager.vider(Me, Partager.nonInitial)
                    total()
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")

                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر المبلغ من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub



    Private Sub Comb_marji3ona_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Partager.vider(Me, Partager.nonInitial)
        Try
            Partager.deClt = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deClt.Key) Then
                    dv = d
                    dv.remplir_List_montant()
                End If
            Next
            List_tous_matloub.Items.Clear()
            For Each khib As Montant In dv.List_montant
                If khib.Type = "المبلغ المطلوب" Then
                    List_tous_matloub.Items.Add(New ListViewItem(New String() {khib.Id, khib.Montant, khib.Date_payement, khib.Remarque}))
                End If
            Next

            '----------------------------------------------------------------------------------------------
            List_tous_mouda3.Items.Clear()
            For Each khib As Montant In dv.List_montant
                If khib.Type = "المبلغ المودع" Then
                    List_tous_mouda3.Items.Add(New ListViewItem(New String() {khib.Id, khib.Montant, khib.Date_payement, khib.Remarque}))
                End If
            Next

            '------------------------
            total()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click
        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name
    End Sub
End Class